package ro.peoplecentric.ctc.faces.model;

import java.util.Arrays;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import ro.peoplecentric.ctc.common.entity.Category;
import ro.peoplecentric.ctc.common.entity.Item;
import ro.peoplecentric.ctc.common.entity.ItemType;
import ro.peoplecentric.ctc.common.entity.Region;
import ro.peoplecentric.ctc.common.exception.ApplicationException;
import ro.peoplecentric.ctc.common.service.CategoryService;
import ro.peoplecentric.ctc.common.service.ItemService;
import ro.peoplecentric.ctc.common.service.RegionService;

/**
 * Item model bean.
 *
 * @author pguzun
 */
@Controller
@Scope("view")
public class ItemBean extends BaseModelBean {
    
    protected static final long serialVersionUID = 13L;

    @Resource
    private RegionService regionService;
    @Resource
    private CategoryService categoryService;
    @Resource
    private ItemService itemService;
    //
    private Item currentObject;
    //
    private List<Region> regions;
    private List<Category> categories;
    private List<ItemType> types;

    public ItemBean() {
    }

    @SuppressWarnings("unused")
    @PostConstruct
    private void init() {
        currentObject = new Item();
        types = Arrays.asList(ItemType.Business, ItemType.Individual);
        try {
            regions = regionService.getAll();
            categories = categoryService.getAll();
        } catch (ApplicationException e) {
            getLogger().error(e.getMessage(), e);
            getMessagePublisher().addErrorMessage("msg.general.load.fail");
        }
    }

    /**
     * Saves new {@link Item} to DB.
     */
    public String add() {
        try {
            itemService.saveItem(getCurrentObject());
            setCurrentObject(null);
            getMessagePublisher().addInfoMessage("itemAddPage.save.succes");
        } catch (ApplicationException e) {
            getLogger().error(e.getMessage(), e);
            getMessagePublisher().addErrorMessage("msg.general.fail");
        }
        return "item-list";
    }

    /**
     * Saves {@link Item} to DB.
     */
    public String update() {
        try {
            itemService.saveItem(getCurrentObject());
            setCurrentObject(null);
            getMessagePublisher().addInfoMessage("itemUpdatePage.save.succes");
        } catch (ApplicationException e) {
            getLogger().error(e.getMessage(), e);
            getMessagePublisher().addErrorMessage("msg.general.fail");
        }
        return "item-list";
    }

    /**
     * @return the regions
     */
    public List<Region> getRegions() {
        return regions;
    }

    /**
     * @return the currentObject
     */
    public Item getCurrentObject() {
        return currentObject;
    }

    /**
     * @param item the currentObject to set
     */
    public void setCurrentObject(Item item) {
        this.currentObject = item;
    }

    /**
     * @return the categories
     */
    public List<Category> getCategories() {
        return categories;
    }

    /**
     * @param categories the categories to set
     */
    public void setCategories(List<Category> categories) {
        this.categories = categories;
    }

    /**
     * @return the types
     */
    public List<ItemType> getTypes() {
        return types;
    }
}
